/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package br.liveo.ui;

import br.liveo.navigationliveo.ResourceTable;
import br.liveo.utils.ToastUtil;

import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import ohos.app.Context;

import java.lang.reflect.Method;

/**
 * ToolBar
 */
public class ToolBar extends DependentLayout {

    /**
     * toolBarTow
     */
    public DependentLayout toolBarTow;
    /**
     * toolBarOne
     */
    public DependentLayout toolBarOne;

    private Component myComponent;
    private Image threeHorizontal;
    private Text tooBarTitle;
    private Image search;
    private Image add;
    private Image back;
    private TextField textField;
    private Image delete;
    private Image more;
    private Button addButton;
    private boolean isToolBarOne = true;

    public ToolBar(Context context) {
        this(context, null);
    }

    public ToolBar(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public ToolBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        myComponent = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_navigation_tool_bar, null, false);
        initComponent();
        setComponentPixMap();
        initListener();
        addComponent(myComponent);
    }

    private void initComponent() {
        // toolBar_one
        toolBarOne = (DependentLayout) myComponent.findComponentById(ResourceTable.Id_tooBar_one);
        threeHorizontal = (Image) toolBarOne.findComponentById(ResourceTable.Id_leftImage_one);
        tooBarTitle = (Text) toolBarOne.findComponentById(ResourceTable.Id_tooBarTitle_one);
        search = (Image) toolBarOne.findComponentById(ResourceTable.Id_rightImage_one);
        add = (Image) toolBarOne.findComponentById(ResourceTable.Id_rightImageTwo_one);
        // toolBar_tow
        toolBarTow = (DependentLayout) myComponent.findComponentById(ResourceTable.Id_tooBar_tow);
        back = (Image) toolBarTow.findComponentById(ResourceTable.Id_leftImage_tow);
        textField = (TextField) toolBarTow.findComponentById(ResourceTable.Id_tooBarInput_tow);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(31, 122, 204));
        textField.setCursorElement(shapeElement);
        delete = (Image) toolBarTow.findComponentById(ResourceTable.Id_rightImage_tow);
        addButton = (Button) toolBarTow.findComponentById(ResourceTable.Id_addButton);
        delete.setClickedListener(component -> {
            textField.setText("");
            textField.requestFocus();
            showSoftInput();
        });
        textField.addTextObserver((s1, i, i1, i2) -> {
            if (s1.length() > 0) {
                delete.setVisibility(Component.VISIBLE);
            } else {
                delete.setVisibility(Component.HIDE);
            }
        });
        more = (Image) toolBarTow.findComponentById(ResourceTable.Id_rightImageTwo_tow);
    }

    private void setComponentPixMap() {
        // toolBar_one
        threeHorizontal.setPixelMap(ResourceTable.Media_more);
        tooBarTitle.setText("nDrawer");
        search.setPixelMap(ResourceTable.Media_ic_search_white_24dp);
        add.setPixelMap(ResourceTable.Media_ic_add_white_24dp);
        // toolBar_tow
        back.setPixelMap(ResourceTable.Media_back);
        delete.setPixelMap(ResourceTable.Media_delete);
        more.setPixelMap(ResourceTable.Media_more_tow);
    }

    private void initListener() {
        search.setClickedListener(component -> {
            if (textField.getText().length() > 0) {
                textField.setText("");
            }
            toolBarOne.setVisibility(Component.HIDE);
            setToolBarOne(false);
            toolBarTow.setVisibility(Component.VISIBLE);
            textField.requestFocus();
            showSoftInput();
            ToastUtil.show(mContext, "Search");
        });
        back.setClickedListener(component -> {
            toolBarOne.setVisibility(Component.VISIBLE);
            setToolBarOne(true);
            toolBarTow.setVisibility(Component.HIDE);
            textField.clearFocus();
        });
        more.setClickedListener(component -> {
            addButton.setWidth(0);
            addButton.setVisibility(Component.VISIBLE);
            more.setVisibility(Component.HIDE);
            more.setEnabled(false);
            AnimatorValue animatorValue = new AnimatorValue();
            animatorValue.setDuration(100);
            animatorValue.setValueUpdateListener((animatorValue1, v1) -> addButton.setWidth((int) (550 * v1)));
            animatorValue.start();
        });
    }

    public void setToolBarOne(boolean isShow) {
        this.isToolBarOne = isShow;
    }

    public boolean getToolBarOne() {
        return this.isToolBarOne;
    }

    private void showSoftInput() {
        try {
            Class inputClass = Class.forName("ohos.miscservices.inputmethod.InputMethodController");
            Method method = inputClass.getMethod("getInstance");
            Object object = method.invoke(new Object[]{});
            Method startInput = inputClass.getMethod("startInput", int.class, boolean.class);
            startInput.invoke(object, 0, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
